import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  //设置默认首页
  {
    path:'/',
    redirect: 'home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children:[
      {
        path: '/main', //首页
        name: 'main',
        component: () => import('../views/Main'),
      },
      //组件
      {
        path: '/Assembly/editor', //富文本
        name: 'editor',
        component: () => import('../views/Assembly/Editor'),
      },
      {
        path: '/Assembly/UploadPictures', //上传
        name: 'UploadPictures',
        component: () => import('../views/Assembly/UploadPictures'),
      },
      //管理
      {
        path: '/manage/user',  //用户
        name: 'manageUser',
        component: () => import('../views/Manage/User')
      },
      {
        path: '/manage/admin',  //管理员
        name: 'manageAdmin',
        component: () => import('../views/Manage/Admin')
      },
      {
        path: '/manage/shop',  //商店
        name: 'manageShop',
        component: () => import('../views/Manage/Shop')
      },
      {
        path: '/manage/discuss',  //点评
        name: 'manageDiscuss',
        component: () => import('../views/Manage/Discuss')
      },
      {
        path: '/manage/checkShop',  //审核
        name: 'managecheckShopManage',
        component: () => import('../views/Manage/CheckShop')
      },
      //异常
      {
        path: '/403',
        name: '403',
        component: () => import('../views/Abnormal/403')
      },
      {
        path: '/404',
        name: '404',
        component: () => import('../views/Abnormal/404')
      },
      {
        path: '/500',
        name: '500',
        component: () => import('../views/Abnormal/500')
      },
      //个人
      {
        path: '/person/settings', //个人设置
        name: 'settings',
        component: () => import('../views/Person/Settings')
      },
      {
        path: '/person/center', //个人中心
        name: 'center',
        component: () => import('../views/Person/Center')
      },
      //其他
      {
        path: '/other', 
        name: 'other',
        component: () => import('../views/Other')
      },
      {
        path: '/page1', //页面1
        name: 'page1',
        component: () => import('../views/Page1')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 解决vue-router在3.0版本以上重复点报错问题
// 法一:本项目无效
// const VueRouterPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (to) {
//   return VueRouterPush.call(this, to).catch(err => err)
// }
// 法二:本项目有效
let originPush=VueRouter.prototype.push;
let originReplace=VueRouter.prototype.replace;
//重写push/replace
//第一个参数：告诉原来的push方法，往哪里跳转（传递哪些参数）
//第二个参数：成功的回调
//第三个参数：失败的回调
//call/apply区别
//相同点：都可以调用函数一次，都可以篡改函数的上下文(this）一次‘
//不同点：call/apply传递参数：call传递参数用逗号隔开，apply方法执行，传递数组
VueRouter.prototype.push=function(location,resolve,reject){
    if(resolve&&reject){
        originPush.call(this,location,resolve,reject);
    }else{
        originPush.call(this,location,()=>{},()=>{});
    }
}
VueRouter.prototype.replace=function(location,resolve,reject){
    if(resolve&&reject){
        originReplace.call(this,location,resolve,reject);
    }else{
        originReplace.call(this,location,()=>{},()=>{});
    }
}


export default router
